package com.scau.dao.Impl;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.apache.ibatis.session.SqlSession;
import org.springframework.stereotype.Repository;

import com.alibaba.fastjson.JSONObject;
import com.scau.dao.StudentDao;
import com.scau.mapper.StudentMapper;
import com.scau.pojo.Student;
import com.scau.util.SqlSessionFactoryUtil;

@Repository
public class StudentDaoImpl implements StudentDao{
	
	SqlSession session = SqlSessionFactoryUtil.getSession();
	StudentMapper studentMapper=session.getMapper(StudentMapper.class);
	@Override
	public List<Student> query() {
		
		return studentMapper.selectByExample(null);
	}
	@Override
	public int add(Student student) {
		return studentMapper.insertSelective(student);//返回1则插入成功
	}
	@Override
	public int delete(int stId) {
		return studentMapper.deleteByPrimaryKey(stId);
	}
	@Override
	public int update(Student student) {
		return studentMapper.updateByPrimaryKeySelective(student);
	}
	@Override
	public Map<String,Object> getStudentByName(String stName) {
		Map<String,Object> map=new HashMap<String,Object>();
		try {
			Student student=studentMapper.selectByName(stName);
			if(student==null){
				map.put("returnCode", -1);
				map.put("student", null);
			}else{
				map.put("returnCode", 1);
				map.put("student", student);
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
		return map;
	}
	@Override
	public int isExist(String stName) {
		int result=-1;
		Student student=new Student();
		try {
			student=studentMapper.selectByName(stName);
			if(student!=null){
				result=1;
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
		return result;
	}
	
	
}
